package com.webex.ta.hydra.gui.macro;

import com.webex.ta.hydra.core.ParamDescriptor;
import com.webex.ta.hydra.core.ParamType;

import javax.swing.table.AbstractTableModel;
import java.util.Vector;


/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-9-13
 * Time: 16:49:17
 */
public class MacroFieldModel extends AbstractTableModel {
    private Vector<ParamDescriptor> fields = new Vector<ParamDescriptor>();

    private static final int COLUMN_NAME = 0;
    private static final int COLUMN_TYPE = 1;
    private static final int COLUMN_OPTIONAL = 2;
    private static final int COLUMN_DESCRIPTION = 3;

    public void reset() {
        fields = new Vector<ParamDescriptor>();
        this.fireTableDataChanged();
    }

    public int getRowCount() {
        return fields.size();
    }

    public int getColumnCount() {
        return 4;
    }

    public Object getValueAt(int row, int column) {
        if (row < 0 || row>=fields.size())
            return null;

        ParamDescriptor des = fields.get(row);
        switch (column) {
            case COLUMN_NAME:
                return des.getName();
            case COLUMN_TYPE:
                return des.getType();
            case COLUMN_OPTIONAL:
                return des.isOptional();
            case COLUMN_DESCRIPTION:
                return des.getDescription();
        }
        return des;
    }

    public Class getColumnClass(int col) {
        switch (col) {
            case COLUMN_NAME:
                return String.class;
            case COLUMN_TYPE:
                return ParamType.class;
            case COLUMN_OPTIONAL:
                return Boolean.class;
            case COLUMN_DESCRIPTION:
                return String.class;
        }
        return ParamDescriptor.class;
    }

    public String getColumnName(int col) {
        switch (col) {
            case COLUMN_NAME:
                return "Parameter name";
            case COLUMN_TYPE:
                return "Type";
            case COLUMN_OPTIONAL:
                return "Optional?";
            case COLUMN_DESCRIPTION:
                return "Description";
        }
        return "";
    }

    public boolean isCellEditable(int row, int column) {
        return true;
    }

    public void setValueAt(Object value, int row, int col) {
        switch (col) {
            case COLUMN_NAME:
                fields.get(row).setName(value.toString());
                break;
            case COLUMN_TYPE:
                fields.get(row).setType((ParamType)value);
                break;
            case COLUMN_OPTIONAL:
                fields.get(row).setOptional((Boolean)value);
                break;
            case COLUMN_DESCRIPTION:
                fields.get(row).setDescription(value.toString());
                break;
        }
    }

    public void setFields(Vector<ParamDescriptor> fields) {
        this.fields = fields;
        this.fireTableDataChanged();
    }

    public void removeElementAt(int idx) {
        fields.remove(idx);
    }

    /**
     *
     * @param idx
     * @param direction -1 to move up and 1 to move down
     */
    public boolean move(int idx, int direction ) {
        if (idx < 0 || idx >= fields.size()) // invalid index
            return false;

        if ((idx == 0 && direction<=0) || (idx == fields.size() -1 && direction >=0))
            // try to move up first row  or  move down last row
            return false;

        ParamDescriptor des = fields.get(idx);
        fields.remove(idx);
        if (direction<0)
            fields.insertElementAt(des, idx-1);
        else if (direction > 0)
            fields.insertElementAt(des, idx+1);
        return true;
    }
}
